package com.cyan.app.workflow.activity.SFC;

import android.os.AsyncTask;

import com.cyan.app.workflow.AppContext;
import com.cyan.app.workflow.bean.DDPoolListBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.util.network.GetRequestParams;
import com.alibaba.fastjson.JSON;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
 * Created by DXW on 2016/12/21.
 * 滴滴物流数据获取类
 */

public class DDDataFetcher {
    private static final String REQUEST_URL = HttpConfig.HTTP_MAIN_SERVER_URL;
    public static final int RESULT_SUCCESS = 1;
    public static final int RESULT_FAILED_CANCEL = 2;
    public static final int RESULT_FAILED_SERVER = 3;
    public static final int RESULT_FAILED_EXCEPTION = 4;
    private AsyncTask<String, Integer, String> mPoolsTask = null;
    private List<PoolsObserver> poolsObserverList = new ArrayList<PoolsObserver>();
    private static DDDataFetcher sLiveDataFetcher = null;
    //初始化
    public static DDDataFetcher getInstance() {
        if (sLiveDataFetcher == null) {
            synchronized (DDDataFetcher.class) {
                if (sLiveDataFetcher == null) {
                    sLiveDataFetcher = new DDDataFetcher();
                }
            }
        }
        return sLiveDataFetcher;
    }
    /*
    * 定义物流池观察者
     */
    public interface PoolsObserver {
        void poolsFetchBegin();
        void poolsFetchEnd(int resultCode, DDPoolListBean poolList);
    }

    //注册物流池观察者
    public void registPoolsObserver(PoolsObserver poolsObserver) {
        if (poolsObserver != null && !poolsObserverList.contains(poolsObserver)) {
            poolsObserverList.add(poolsObserver);
        }
    }

    //移除物流池观察者
    public void unRegistPoolsObserver(PoolsObserver poolsObserver) {
        if (poolsObserver != null) {
            poolsObserverList.remove(poolsObserver);
        }
    }

    //通知观察者开始
    private void notifyPoolsObserverBegin() {
        for (int i = 0; i < poolsObserverList.size(); i++) {
            poolsObserverList.get(i).poolsFetchBegin();
        }
    }

    //通知观察者结束
    private void notifyPoolsObserverEnd(int resultCode, DDPoolListBean poolListBean) {
        for (int i = 0; i < poolsObserverList.size(); i++) {
            poolsObserverList.get(i).poolsFetchEnd(resultCode, poolListBean);
        }
    }

    public boolean fetchPoolList(final int type, final String timestamp) {
        try {
            String action = "";
            JSONObject param = new JSONObject();
            param.put("Type", "0");
            if (type == DDActivity.FILTER_SUB_TYPE_LIVE_WL) {
                action = "221";
                fetchPoolList(action, param.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    public boolean fetchPoolList(final int type, final HashMap<String,String> map) {
        try {
            String action = "";
            JSONObject param = new JSONObject();
            param.put("Type", "0");
            if(map.containsKey("Prosscode")){
                param.put("Prosscode", map.get("Prosscode"));
            }
            if(map.containsKey("Manno")){
                param.put("Manno", map.get("Manno"));
            }
            if (type == DDActivity.FILTER_SUB_TYPE_LIVE_WL) {
                action = "221";
                fetchPoolList(action, param.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    public boolean fetchPoolList(final int type, String userno, final String timestamp) {
        try {
            String action = "";
            JSONObject param = new JSONObject();
            param.put("userno", userno);
            param.put("timestamp", timestamp);
            if (type == DDActivity.FILTER_SUB_TYPE_MY_WL) {
                action = "223";
                fetchPoolList(action, param.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean fetchPoolList(final String action, final String param) {
        boolean canFetch = false;
        if (mPoolsTask == null || mPoolsTask.getStatus() == AsyncTask.Status.FINISHED) {
            canFetch = true;
        } else {
            return false;
        }
        notifyPoolsObserverBegin();
        mPoolsTask = new AsyncTask<String, Integer, String>() {
            @Override
            protected String doInBackground(String... params) {
                try {
                    String result = GetRequestParams.getRequestResult(REQUEST_URL, action, param, AppContext.getInstance().getApplicationContext());
                    return result;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
            @Override
            protected void onPostExecute(String s) {
                int resultCode = RESULT_FAILED_SERVER;
                DDPoolListBean poolListBean = new DDPoolListBean();
                if (!s.isEmpty()) {
                    try {
                        poolListBean = JSON.parseObject(s, DDPoolListBean.class);
                        resultCode = RESULT_SUCCESS;
                    } catch (Exception e) {
                        resultCode = RESULT_FAILED_EXCEPTION;
                        e.printStackTrace();
                    }
                }
                notifyPoolsObserverEnd(resultCode, poolListBean);
            }
            @Override
            protected void onCancelled(String s) {
                notifyPoolsObserverEnd(RESULT_FAILED_CANCEL, null);
            }
        }.execute();
        return canFetch;
    }
}
